//
//  YMPhotoPreviewVC.swift
//  YMMedical
//
//  Created by y s on 2023/7/5.
//

import UIKit

@objcMembers class YMPhotoPreviewVC: YMBaseViewController {

    lazy var btnBack: UIButton = {
        let btn = UIButton(type: .custom)
//        btn.setImage(UIImage(systemName: "arrow.left"), for: .normal)
        btn.layer.cornerRadius = SFitUtil.fit(21)
        btn.addTarget(self, action: #selector(clickBackAction), for: .touchUpInside)
        return btn
    }()
    lazy var btnError: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("异常上报", for: .normal)
        btn.setTitleColor(.blue, for: .normal)
        btn.titleLabel?.font = SFitUtil.fitFont(15)
        btn.addTarget(self, action: #selector(clickErrorAction(_:)), for: .touchUpInside)
        return btn
    }()
    lazy var imgvPre: UIImageView = {
        let result = UIImageView()
        result.contentMode = .scaleAspectFill
        return result
    }()
    
    
    public var imgData: Data?
    public var tcTime = 0 //调参数耗时
    var timer: Timer?
    var uploadTime = 0 //上传时间
    var imageUrl: String?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let alert = YSAlert()
        alert.outsideClose = false
        alert.title = YMBridging.kLanguage("sure_upload_txt")
        alert.sureTitle = YMBridging.kLanguage("ok_txt")
        alert.cancelTitle = YMBridging.kLanguage("cancle_txt")
        alert.sureBlock = {[weak self] in
            self?.uploadPhoto(self?.imgData ?? Data())
        }
        alert.cancelBlock = {[weak self] in
            self?.clickBackAction()
        }
        alert.show()
    }
    
    func setUI() {
        navBarHidden = true
        view.backgroundColor = STheme.D8
        view.addSubview(imgvPre)
        view.addSubview(btnError)
        view.addSubview(btnBack)
        
        imgvPre.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        btnError.snp.makeConstraints { make in
            make.centerY.equalTo(btnBack)
            make.right.equalTo(SFitUtil.fit(-15))
        }
        btnBack.snp.makeConstraints { make in
            make.left.equalTo(SFitUtil.fit(15))
            make.top.equalTo(SFitUtil.fit(10)+StatusBarHeight)
            make.size.equalTo(SFitUtil.fitSize(42, height: 42))
        }
        if imgData != nil {
            imgvPre.image = UIImage(data: imgData!, scale: 1.0)
        }
    }
    
    
    private func uploadPhoto(_ imgData: Data){
        
        SHUD.showDarkWait(view, text: YMBridging.kLanguage("genrating_report_txt"))
        
        let fileName = "\(Date().timeIntervalSince1970)_" + UUID().uuidString + ".dng"
        uploadTime = 0
        releaseTimer()
        
//        timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(countDownTimeAction), userInfo: nil, repeats: true)
        
//        YMAliYunUtil.shared().aliyunUploadFile(imgData, fileName: fileName) { [weak self] urlStr in
//            self?.releaseTimer()
//            self?.imageUrl = urlStr
//            self?.requestOperateReportEx(message: "", urlStr: self?.imageUrl ?? "", false)
//        } failure: { [weak self] in
//            self?.releaseTimer()
//            SHUD.hide(self?.view)
//            SHUD.showInfo("上传失败")
//            self?.clickBackAction()
//        }
//    }
    
//    func requestOperateReportEx(message: String, urlStr: String, _ isReport: Bool) {
//        YMHomeService.appOperateReportEx(withExCondition: message, imageFile: urlStr, fileSize:YMDeviceTools.transformDataLength(imgData?.count ?? 0), tcTime: tcTime, uploadTime: uploadTime) { [weak self] in
//            SHUD.hide(self?.view)
//            if (isReport){
//                SHUD.showInfo("异常反馈成功")
//            }else{
//                SHUD.showInfo("上传成功")
//            }
//        } failure: {[weak self] message in
//            SHUD.hide(self?.view)
//            SHUD.showInfo(message)
//        }
    }

    
    
    @objc private func countDownTimeAction(){
        uploadTime += 1
    }
    
    @objc private func clickBackAction(){
        SJumpUtil.popVC(self, animated: true)
    }
    
    @objc private func clickErrorAction(_ sender: UIButton){
        let alert = YMHomeErrorUploadAlert()
        alert.uploadBlock = {[weak self] message in
//            self?.requestOperateReportEx(message: message, urlStr: self?.imageUrl ?? "", true)
        };
        alert.show(view)
    }
    
    func releaseTimer(){
        if timer != nil {
            timer?.invalidate()
            timer = nil
        }
    }
    deinit {
        releaseTimer()
    }
    
}
